import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:test_performance/res/assets_res.dart';

/*
 * @Author: zhudaihao
 * @Date: 2022/3/16
 * @Describe: 性能分析--实战
 */

void main() {
  //观测台里面显示build构建数
   debugProfileBuildsEnabled = true;
  //观测台里面显示Paints构建数
  debugProfilePaintsEnabled=true;
  //观测台里面显示Layout构建数
  debugProfileLayoutsEnabled=true;
  runApp(Test7TimeLine());
}

class Test7TimeLine extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _Test7TimeLineState();
  }
}

class _Test7TimeLineState extends State<Test7TimeLine> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ///app显示性能统计数据
      // showPerformanceOverlay: true,
      ///gpu 多图层
      checkerboardOffscreenLayers: true,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("性能分析--实战"),
        ),
        body: content(),
      ),
    );
  }

  Widget content() {
    Widget result = Column(
      children: <Widget>[
        //颜色横条
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            height: 80,
            color: Colors.blue,
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            height: 80,
            color: Colors.cyan,
          ),
        ),
        //三个图片
        Container(
          margin: EdgeInsets.only(top: 30, bottom: 30),
          height: 100,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                child: Image.asset(AssetsRes.RUNNER),
              ),
              Container(
                child: Image.asset(AssetsRes.RUNNER),
              ),
              Container(
                child: Image.asset(AssetsRes.RUNNER),
              ),
            ],
          ),
        ),
        //计数
        Container(
          height: 180,
          color: Colors.amber,
          child: const Center(
            child: MyText(),
          ),
        )
      ],
    );
    return result;
  }
}

///1 build优化思路(提高build效率,降低widget tree遍历的出发点，将setState刷新数据尽量下发到底层节点)
class MyText extends StatefulWidget {
  const MyText({Key? key}) : super(key: key);

  @override
  _MyTextState createState() => _MyTextState();
}

class _MyTextState extends State<MyText> {
  int countValue = 0;
  Timer? _timer;

  // 启动Timer 定时器
  void _startTimer() {
    final Duration duration = Duration(seconds: 1);
    _timer = Timer.periodic(duration, (timer) {
      setState(() {
        countValue++;
      });
      print(countValue);
    });
  }

  @override
  void initState() {
    super.initState();
    _startTimer();
  }

  @override
  void dispose() {
    super.dispose();
    _timer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(child: Text('$countValue'));
  }
}
